package model;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;

import main.GameCanvas;
import screen.PaintPopup;
import utilities.GameResource;
import font.BitmapFont;

public class PlayerInfo extends ItemInfo {
	private int m_iMoneyAdd[];
    private int m_deltaAdd;
	public Animal m_defaultPet;
	public AvatarInfo m_defaultAvatar = null;
	public int m_avatarId;

	public boolean m_onlineStatus;
    public int m_level;
    
    //public String forumUrl;
    //public String encriptKey;
    //public String password;
    public String fullName;
    public String status;
    public String mobile;
    
    public String m_UserName;
    
    public int gender;
	public boolean isReady;
    public boolean isOwner;
    
    public boolean m_bIsDrawRichest = false;
        
    public Vector m_vtChat = new Vector();
    public int m_iTimeChat = 0;
    	
	public void chat(String content){
		m_vtChat.removeAllElements();
		
		m_iTimeChat = 0;
		String[] arr = BitmapFont.m_bmNormalFont.splitFontBStrInLine(content, 60);
		int i;
		for(i = 0; i < arr.length; i++){
			m_vtChat.addElement(arr[i]);
		}
		arr = null;
	}
	
    public PlayerInfo() {
        m_iMoneyAdd = new int[2];
        m_iMoneyAdd[0] = 0;			//gold
        m_iMoneyAdd[1] = 0;			//dina
    }
    
	public MyObj clone() {
		// TODO Auto-generated method stub
		return null;
	}

	public void paint(Graphics g, int x, int y) {
		// TODO Auto-generated method stub
	}

	public void setAvatar(int avaId){
		m_defaultAvatar = DataManager.gI().getAvatarInfo(avaId);
		if(m_defaultAvatar == null){
			m_defaultAvatar = DataManager.gI().getAvatarInfo(1);
		}
	}
	
	public int getAvatarId(){
		if(m_defaultAvatar != null){
			return m_defaultAvatar.m_iId;
		} 
		else return 1;
	}
	
	public void paintIcon(Graphics g, int x, int y) {
		// TODO Auto-generated method stub
		if(m_defaultAvatar != null)
			m_defaultAvatar.paintIcon(g, x, y);	
	}
	
	public void paintAvatarInBoard(Graphics g, int x, int y, int w, int h){
		g.setColor(0x6477ac);
		g.fillRect(x, y, 32, 32);
		if(m_defaultAvatar != null)
			m_defaultAvatar.paintIcon(g, x, y);
	    PaintPopup.paintBox(g, x + 32, y + 2, w - 32, 28);
	    BitmapFont.drawNormalFont(g, m_strName, x + 35, y + 5, 0x000000, Graphics.LEFT | Graphics.TOP);
	    BitmapFont.drawMoney(g,"" + m_iMoney[0], x + 35, y + 20, Graphics.LEFT | Graphics.TOP);
	}
	
	public void paintInBoard(Graphics g, int x, int y, int direction, boolean isDisplayAnimal) {
		// TODO Auto-generated method stub
		if(isDisplayAnimal){
			//paint box name
			int color1 = 0x72beed;
			int color2 = 0x2b6589;
			if(isOwner){
				color1 = 0xffd56e;
				color2 = 0x8b4710;
			}
			
			PaintPopup.paintBox(g, x, y, 60, 15, 1, color1, color2);
			PaintPopup.paintHealtyBar(g, x + 5, y + 6, m_defaultPet.animalInfo.m_sHealth, m_defaultPet.m_iHealth, m_defaultPet.m_iBonusHealth);
			
			//paint box avatar
			PaintPopup.paintBox(g, x, y + 14, 60, 40, 1, 0xffffff, 0x2b6589);
			
			if(m_vtChat.size() > 0){
				//paint chat box
				int heightChat = 15 * m_vtChat.size();
				int yPopup = y - heightChat;
				PaintPopup.paintBox(g, x, yPopup, 80, heightChat, 1, 0xffffff, 0x72beed);
				int i, count = m_vtChat.size();
				for(i = 0; i < count; i++){
					String str = (String)m_vtChat.elementAt(i);
					BitmapFont.drawNormalFont(g, str, x + 5, yPopup + 15 * i, 0x000000, Graphics.LEFT | Graphics.TOP);
				}
				
				m_iTimeChat++;
				if(m_iTimeChat > 50){
					m_vtChat.removeAllElements();
				}
			}
			else
			{
				//paint avatar
				if(m_defaultAvatar != null)
					m_defaultAvatar.paintIcon(g, x + 2, y + 15);
				
				//paint level
				PaintPopup.paintLevel(g, x + 33, y + 35, "" + m_level, 0x3a001e);
				//paint name
				BitmapFont.drawBoldFont(g, m_strName, x + 33, y + 20, 0x3a001e, Graphics.VCENTER|Graphics.LEFT);
			}
			
			if(m_defaultPet != null){//chi ve thu doi voi nhung game co lien quan den thu
				m_defaultPet.paintAction(g, 1, Animal.STAND, x + ((direction == 1)? 30: 0), y + 100, direction);
			}
			if(isOwner){
				//22
				GameResource.instance.m_frameIconItems.drawFrame(21, x + 30, y + 55, 0, g);
			}else if(isReady){
				GameResource.instance.m_frameIconItems.drawFrame(22, x + 30, y + 55, 0, g);
			}
		}
		else
		{
			//paint box name
			int color1 = 0x72beed;
			int color2 = 0x2b6589;
			if(isOwner){
				color1 = 0xffd56e;
				color2 = 0x8b4710;
			}
			
			PaintPopup.paintBox(g, x, y, 60, 15, 1, color1, color2);
			BitmapFont.drawBoldFont(g, m_strName, x + 30, y + 7, 0x3a001e, Graphics.VCENTER|Graphics.HCENTER);
			
			//paint box avatar
			PaintPopup.paintBox(g, x, y + 14, 60, 40, 1, 0xffffff, 0x2b6589);
			if(isOwner){
				//22
				GameResource.instance.m_frameIconItems.drawFrame(21, x + 30, y + 15, 0, g);
			}else if(isReady){
				GameResource.instance.m_frameIconItems.drawFrame(22, x + 30, y + 15, 0, g);
			}
			
			//paint avatar
			if(m_defaultAvatar != null)
				m_defaultAvatar.paintIcon(g, x + 2, y + 15);
			
			//paint level
			PaintPopup.paintLevel(g, x + 33, y + 35, "" + m_level, 0x3a001e);
			
			if(m_vtChat.size() > 0){
				//paint chat box
				int heightChat = 15 * m_vtChat.size();
				int yPopup = y - heightChat;
				PaintPopup.paintBox(g, x, yPopup, 80, heightChat, 1, 0xffffff, 0x72beed);
				int i, count = m_vtChat.size();
				for(i = 0; i < count; i++){
					String str = (String)m_vtChat.elementAt(i);
					BitmapFont.drawNormalFont(g, str, x + 5, yPopup + 15 * i, 0x000000, Graphics.LEFT | Graphics.TOP);
				}
				
				m_iTimeChat++;
				if(m_iTimeChat > 50){
					m_vtChat.removeAllElements();
				}
			}
		}
	}

	public void paintInRow(Graphics g, int x, int y, int width, int height, boolean isFocus) {
		// TODO Auto-generated method stub
		int midHeight = height / 2;

		int tmp = y + (height - 32) / 2;
        g.setColor(0xffffff);
        g.fillRect(x, tmp, 32, 32);

        if(m_defaultAvatar != null)
			m_defaultAvatar.paintIcon(g, x, tmp);
		
        //paint online status
        if (m_onlineStatus) {
           g.setColor(0x3c8dc0);        	
        } else {
        	g.setColor(0xf84135);
        }
        g.fillRoundRect(x + width - 30, y + midHeight - 12, 10, 10, 10, 10);

        //paint name + level
        x += height;
        
        BitmapFont.drawBoldFont(g, m_strName, x, y + midHeight - 12, 0x3a001e, Graphics.LEFT | Graphics.VCENTER);
        if(m_bIsDrawRichest)
        	BitmapFont.drawNormalFont(g, GameResource.gold + ": " + m_iMoney[0], x, y + midHeight+3, 0x3a001e, Graphics.LEFT | Graphics.VCENTER);
        else{
        	//paint level
			PaintPopup.paintLevel(g, x, y + midHeight - 5, "" + m_level, 0x3a001e);
			
			if(status == null)
				status = "";
			BitmapFont.drawNormalFont(g, "Status: "+status, x, y + midHeight + 12, 0x3a001e, Graphics.LEFT | Graphics.VCENTER);
        	//BitmapFont.drawNormalFont(g, GameResource.level + ": " + m_level, x, y + midHeight + 5, 0x3a001e, Graphics.LEFT | Graphics.VCENTER);
		}
        
    }
	public void paintInTopBar(Graphics g, int x, int y){
		int height = GameCanvas.hBottomBar;
		g.setColor(0x519dcd);
		g.fillRect(63+5, 0, GameCanvas.w - 63-5, height - 1);
		g.fillRect(0, 0, 63+5, 35);
		
		g.setColor(0x3a7092);
		g.drawRect(3, 1, 32, 32);
		
		//paint avatar
		g.setColor(0x6477ac);
		g.fillRect(4, 2, 32, 32);
		if(m_defaultAvatar != null)
			m_defaultAvatar.paintIcon(g, 4, 2);
		
		
		g.setColor(0x72beed);		
		g.drawLine(64+5, height - 1, GameCanvas.w, height -1);
		g.drawLine(63+5, height - 1, 63+5, 35);
	    g.drawLine(0, 35, 63+5, 35);	
	    g.drawLine(62+5, 34, 62+5, 34);
	    
		g.setColor(0x3a7092);
		g.drawLine(64+5, height, GameCanvas.w, height);
		g.drawLine(64+5, height, 64+5, 34);
	    g.drawLine(0, 36, 62+5, 36);	    
	    g.drawLine(62+5, 36, 64+5, 34);
	    
	    paintBoxName(g,m_strName, 40, 2);
	    PaintPopup.paintMoney(g, GameCanvas.w - 105, 3, m_iMoney);
	    PaintPopup.paintLevel(g, 39, 20, ""+m_level, 0xe3ffda);
	}
	int m_ideltaX = 0;
	int m_iDirection = 1;
	public void paintInfo(Graphics g, int x, int y) {
		// TODO Auto-generated method stub			
		///////////
		if(m_defaultAvatar != null){
			m_defaultAvatar.paintIcon(g, x, y);
		}
		
		if(fullName == null)
			fullName = "";
		
        BitmapFont.drawNormalFont(g, GameResource.name + ": " + m_UserName, x+5, y+35, 0x3a001e, Graphics.LEFT);
                
        if(status == null)
        	status = "";
        BitmapFont.drawNormalFont(g, GameResource.blast + ": ", x + 5, y + 50, 0x3a001e, Graphics.LEFT);
        if(status.length() > 20){
        	int widthTxt = BitmapFont.m_bmFont.stringWidth(status);
        	g.setClip(x + 45, y + 50, 105, 25);
        	
        	BitmapFont.drawNormalFont(g, status, x + 45 + m_ideltaX, y + 50, 0x3a001e, Graphics.LEFT);
        	m_ideltaX = m_ideltaX - 1 * m_iDirection;
        	if(m_ideltaX < 105 - widthTxt){
        		m_iDirection = -1;
        	}
        	if(m_ideltaX == 5)
        		m_iDirection = 1;
        }
        else
        	BitmapFont.drawNormalFont(g, status, x + 45, y + 50, 0x3a001e, Graphics.LEFT);
        
        g.setClip(x, y + 50, 105, 180);    	
        if(mobile == null)
        	mobile = "";
        BitmapFont.drawNormalFont(g, GameResource.mobile + ": " + mobile, x+5, y+65, 0x3a001e, Graphics.LEFT);        
        
        String str;
        if(gender == 0)
        	str = "Nam";
        else
        	str ="Nữ";
        
        BitmapFont.drawNormalFont(g, GameResource.gender + ": "+str, x+5, y+80, 0x3a001e, Graphics.LEFT);
        BitmapFont.drawNormalFont(g, GameResource.exp + ": "+m_sExperience, x+5, y+95, 0x3a001e, Graphics.LEFT);
        BitmapFont.drawNormalFont(g, GameResource.level + ": "+m_level, x+5, y+110, 0x3a001e, Graphics.LEFT);
	}
	
	public void paintBoxName(Graphics g, String str, int x, int y){
		g.setColor(0xdeecf5);
		int stringW = BitmapFont.m_bmFont.stringWidth(str);
		g.fillRect(x, y+1, stringW + 8, 14-1);
		g.drawLine(x+1, y, x+stringW + 8 -2, y);
		g.drawLine(x+1, y+14, x+stringW + 8 -2, y+14);		
		int hWidth = x + (stringW + 8)/2;
		int hHeight = y +7;
		BitmapFont.drawBoldFont(g, str, hWidth, hHeight, 0x555759, Graphics.VCENTER|Graphics.HCENTER);
	}	

	//update money animation
    public void update() {
        // TODO Auto-generated method stub
        for (int i = 0; i < 2; i++) {
            if (m_iMoneyAdd[i] != 0) {
                if (Math.abs(m_iMoneyAdd[i]) > Math.abs(m_deltaAdd)) {
                    m_iMoneyAdd[i] -= m_deltaAdd;
                    m_iMoney[i] += m_deltaAdd;
                } else {
                    m_iMoney[i] += m_iMoneyAdd[i];
                    m_iMoneyAdd[i] = 0;
                }
            }
        }
    }
    
    public void setMoney(int money, int type){
    	m_iMoney[type] = money;
    }
    public int getMoney(int type){
    	return m_iMoney[type];
    }

	//add money to player (create animation for money increase)
    public void addMoney(int money, int type) {
        if (money == 0) {
            return;
        }

        m_iMoneyAdd[type] += money;

        m_deltaAdd = m_iMoneyAdd[type] / 5;
        if (m_deltaAdd == 0) {
            m_deltaAdd = 1;
        }
    }

    //minus money to player (create animation for money decrease)
    public void subMoney(int money, int type) {
        if (money == 0) {
            return;
        }

        m_iMoneyAdd[type] -= money;

        m_deltaAdd = m_iMoneyAdd[type] / 5;
        if (m_deltaAdd == 0) {
            m_deltaAdd = -1;
        }
    }
	public int getLevel() {
		// TODO Auto-generated method stub
		return m_level;
	}

	public void setShortName(String userName) {
		// TODO Auto-generated method stub
		if(userName.length() > 5){
			m_strName = userName.substring(0, 5) + "...";
		}
		else
			m_strName = userName;
	}
}
